package packageModel;

import java.io.Serializable;
import java.util.*;

/**
 *
 * @author pimas007
 */
public class Panier implements Serializable{
    
    private HashMap<Long, ItemPanier> myBasket;
    
    public Panier()
    {       
        myBasket = new HashMap();
    }
    
    public HashMap getPanier()
    {
        return myBasket;
    }
    
    public void addItem(Livre book, int qte)
    {
        if (myBasket.containsKey(book.getIsbn()))
        {
            ItemPanier itemTemp = myBasket.get(book.getIsbn());
            int qteTot = itemTemp.getQte() + qte;
            itemTemp.setQte(qteTot);
            myBasket.put(book.getIsbn(), itemTemp);  
        }
        else
        {
            myBasket.put(book.getIsbn(), new ItemPanier(book.getIsbn(), book.getTitre(), book.getNomAuteur(), book.getPrixPromo(), qte));
        }
        
    }
    public void setItem(Long isbn, int qte){
        
        if (qte>0){
            
            ItemPanier itemTemp = myBasket.get(isbn);
            itemTemp.setQte(qte);
            myBasket.put(isbn, itemTemp);
            
        }
        else{
            myBasket.remove(isbn);
        }
        
    }
    
    
    public Double getTotal()
    {       
        double total = 0.00;
        
        for(ItemPanier item : myBasket.values())
        {
            total += item.getQte() * item.getUnitPrice();
        } 
        return total;
    }  
}
